package com.manager.plat.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IPartnerUpperDao;
import com.manager.plat.vo.PartnerUpperVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class PartnerUpperAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6061793094129597690L;

	private static Logger logger = Logger.getLogger(PartnerUpperAction.class);
	
	private IPartnerUpperDao partnerUpperDao;			//上游数据信息
	private List<PartnerUpperVo> partnerUpperVoList;	//上游合作方信息列表
	private PartnerUpperVo partnerUpperVo;				//上游合作方信息VO
	private String actionPath;							//操作成功返回路径
	
	/**获得上游列表*/
	public String getPartnerUpperList(){
		partnerUpperVoList = partnerUpperDao.getPartnerUpperList();
		return SUCCESS;
	}
	/**添加上游信息*/
	public String addPartnerUpper(){
		/**重置上游信息*/
		partnerUpperVo = null;
		
		return SUCCESS;
	}
	/**编辑上游信息*/
	public String editPartnerUpper(){
		if(partnerUpperVo.getId()  != 0){
			partnerUpperVo = partnerUpperDao.getPartnerUpperById(partnerUpperVo.getId());
		}
		
		return SUCCESS;
	}
	/**更新上游信息*/
	public String updatePartnerUpper(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		
		if(partnerUpperVo.getId() == 0){
			partnerUpperVo.setAdd_user(userInfoVo.getId());
			/**插入上游信息*/
			int count = partnerUpperDao.insertPartnerUpper(partnerUpperVo);
			
			if(count > 0){
				logger.info(String.format("[%s]添加[%s]上游成功", userInfoVo.getLogin_username(),partnerUpperVo.getName()));
			}else{
				logger.info(String.format("[%s]添加[%s]上游失败", userInfoVo.getLogin_username(),partnerUpperVo.getName()));
			}
		}else{
			partnerUpperVo.setUpdate_user(userInfoVo.getId());
			int count = partnerUpperDao.updatePartnerUpper(partnerUpperVo);
			if(count > 0){
				logger.info(String.format("[%s]更新[%s]上游成功", userInfoVo.getLogin_username(),partnerUpperVo.getName()));
			}else{
				logger.info(String.format("[%s]更新[%s]上游失败", userInfoVo.getLogin_username(),partnerUpperVo.getName()));
			}
		}
		
		actionPath = "partnerUpper/getPartnerUpperList.action";
		return SUCCESS;
	}
	
	/**删除上游信息*/
	public String deletePartnerUpper(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		int count = partnerUpperDao.deletePartnerUpper(partnerUpperVo.getId(),userInfoVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除[%s]上游成功", userInfoVo.getLogin_username(),partnerUpperVo.getName()));
		}else{
			logger.info(String.format("[%s]删除[%s]上游失败", userInfoVo.getLogin_username(),partnerUpperVo.getName()));
		}
		
		actionPath = "partnerUpper/getPartnerUpperList.action";
		return SUCCESS;
	}
	
	
	public IPartnerUpperDao getPartnerUpperDao() {
		return partnerUpperDao;
	}
	public void setPartnerUpperDao(IPartnerUpperDao partnerUpperDao) {
		this.partnerUpperDao = partnerUpperDao;
	}
	public List<PartnerUpperVo> getPartnerUpperVoList() {
		return partnerUpperVoList;
	}
	public void setPartnerUpperVoList(List<PartnerUpperVo> partnerUpperVoList) {
		this.partnerUpperVoList = partnerUpperVoList;
	}
	public PartnerUpperVo getPartnerUpperVo() {
		return partnerUpperVo;
	}
	public void setPartnerUpperVo(PartnerUpperVo partnerUpperVo) {
		this.partnerUpperVo = partnerUpperVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}
